'use strict'

define(['jquery', 'bootstrap', 'datetimepicker', 'datetimepickerCN', 'select2', 'validate', 'validate.en', 'shiftform',], function($) {
  
  return {
        
    init: function(data) {
        this.initPage(data.week);
        this.arrangeTime();
        this.finish();
        this.searchModal();
    },
    
    initPage: function(week){
        
        $('select[name="siteId"]').select2({
            minimumResultsForSearch : -1,
            allowClear : true,
            width: '150'
          });
        console.log($('select[name="siteId"]').select2('val'))

          $('select[name="site"]').change(function(){ 
            var site = $(this).children('option:selected').val();
            window.location.href = window.__CTX__.root + "/expiry-schedules?site=" + site + "&week=" + week;

          })
    },
    
    // arrangeTime: function(status) {
    // 	var that = this;
    //     $(document).on('shown.bs.modal', '#expiry-schedule-time', function(e){
        	
    //     		var scheduleId = $('input[name="scheduleId"]').val();
            	
    //         	$('select[name="coachId"]').on('change', function() {
    //         		$('.site-item').remove();
    //         		var coachId = $(this).val();
    //         		var isYear = $('input[name="isYear"]').val();
    //         		if(!(!coachId) && isYear == 'false') {
    //         			$.ajax({
    //         				type: 'POST',
    //         				url: window.__CTX__.root + '/expiry-schedules/' + scheduleId + '/coach-site',
    //         				data: {'coachId': coachId},
    //         				success: function(res) {
    //         					res = JSON.parse(res);
    //         					if(res.data.siteAmount > 1) {
    //         						var siteSelect = '';
    //         						for(var index in res.data.sites) {
    //         							siteSelect = siteSelect + '<option value="' + res.data.sites[index].id + '">' + res.data.sites[index].name + '</option>'
    //         						}
    //         						$('.coach-item').after('<div class="form-group x-form-group site-item">' +
    //         								         '<label class="col-xs-3 control-label">门店</label>' + 
    //         								         '<div class="pull-left col-xs-8">' + 
    //         								           '<select name="siteId" class="form-control" data-validate="required">' +
    //         							                   siteSelect +
    //         								         '</div>' +
    //         									  '</div>');
            						
    //         						$('select[name="siteId"]').select2({
    //         						    placeholder : '请选择门店',
    //         						    minimumResultsForSearch : 5,
    //         						    allowClear : false,
    //         						  });
    //         					}
    //         				}
    //         			})
    //         		}
    //         	})
        	
    //         $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
            
    //         var $form = $(e.currentTarget).find("form")                          //bind shiftform
    //         $form.shiftform({
    //           beforeSubmit : function(el) {
    //             $form.trigger("validate.ajax.submit");
    //             return $.bt_validate.result[$.bt_validate.form_id]
    //           }
    //         })
    //     })
    //   },


    arrangeTime: function(status) {
        var that = this;
        $(document).on('shown.bs.modal', '#expiry-schedule-time', function(e){
            
                var scheduleId = $('input[name="scheduleId"]').val();
                //var siteId = $('input[name="siteId"]').val();
                
                $('select[name="siteId"]').on('change', function() {
                    //$('.coach-item').remove();
                    var siteId = $(this).val();
                    var isYear = $('input[name="isYear"]').val();
                    if(!(!siteId) && isYear == 'false') {
                        $.ajax({
                            type: 'POST',
                            url: window.__CTX__.root + '/expiry-schedules/' + siteId + '/site-coaches',
                            data: {'siteId': siteId},
                            success: function(res) {
                                res = JSON.parse(res);
                        
                                if(res.data.coaches &&  res.data.coaches.length> 1) {
                                    var coachSelect = '<option value=""></option>';
                                    for(var index in res.data.coaches) {
                                        coachSelect = coachSelect + '<option value="' + res.data.coaches[index].id + '">' + res.data.coaches[index].name + '</option>'
                                   }


                                $('select[name="coachId"]').empty().append(coachSelect);
                                $('select[name="coachId"]').trigger('change');

                                // $('.coach-item').after('<div class="form-group x-form-group site-item">' +
                                //                  '<label class="col-xs-3 control-label">门店</label>' + 
                                //                  '<div class="pull-left col-xs-8">' + 
                                //                    '<select name="siteId" class="form-control" data-validate="required">' +
                                //                        siteSelect +
                                //                  '</div>' +
                                //               '</div>');
                                    
                                    // $('select[name="siteId"]').select2({
                                    //     placeholder : '请选择门店',
                                    //     minimumResultsForSearch : 5,
                                    //     allowClear : false,
                                    //   }).trigger('change');
                                }
                            }
                        })
                    }
                })
            
            $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
            
            var $form = $(e.currentTarget).find("form")                          //bind shiftform
            $form.shiftform({
              beforeSubmit : function(el) {
                $form.trigger("validate.ajax.submit");
                return $.bt_validate.result[$.bt_validate.form_id]
              }
            })
        })
      },
      
   finish: function(status) {
      $(document).on('shown.bs.modal', '#expiry-schedule-finish', function(e){

    	  $(e.currentTarget).find("form").shiftform()
      })
    },
    
    searchModal: function() {
    	$(document).on('shown.bs.modal', '#expire-search', function(e){
    		$(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate                      
    	})
    }
  }
})
